package com.zkjl.protect_business.controller;

import com.zkjl.protect_business.common.ApiResponse;
import com.zkjl.protect_business.common.SystemLog;
import com.zkjl.protect_business.dto.businessType.BusinessDTO;
import com.zkjl.protect_business.dto.businessType.SpecifiedFieldDTO;
import com.zkjl.protect_business.dto.businessType.UpdateBusinessDTO;
import com.zkjl.protect_business.entity.Business;
import com.zkjl.protect_business.entity.BusinessField;

import com.zkjl.protect_business.repository.BusinessRepository;
import com.zkjl.protect_business.service.BusinessService;
import com.zkjl.protect_business.service.StatisticService;
import com.zkjl.protect_business.vo.BusinessVO;
import com.zkjl.protect_business.vo.statistic.GroupVO;
import com.zkjl.protect_business.vo.statistic.StatGroupDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.junit.jupiter.params.shadow.com.univocity.parsers.annotations.Validate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

import static com.zkjl.protect_business.common.Consts.LOG_TYPE_LOGIN;
import static com.zkjl.protect_business.common.Consts.LOG_TYPE_OPERATION;

@RestController
@RequestMapping("/business")
@Api(tags = {"业务分类"})
public class BusinessController {

    @Autowired
    BusinessService businessService;
    @Autowired
    BusinessRepository businessRepository;
    @Autowired
    StatisticService statisticService;

    @ApiOperation(value = "新增业务分类", notes = "新增业务分类", httpMethod = "POST")
    @PostMapping
    @SystemLog(description = "新增业务分类")
    public ApiResponse<Business> save(@RequestBody @Validated BusinessDTO businessDTO) {

        Business save = businessService.save(businessDTO);
        return ApiResponse.ofSuccess(save);
    }

    @ApiOperation(value = "修改业务分类", notes = "修改业务分类", httpMethod = "PUT")
    @PutMapping(value = "{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "业务分类id", required = true, paramType = "path")
    })
    @SystemLog(description = "修改业务分类")
    public ApiResponse update(@RequestBody @Validated UpdateBusinessDTO updateBusinessDTO,  @PathVariable String id) {
        Business business = businessService.update(updateBusinessDTO, id);
        return ApiResponse.ofSuccess(business);
    }

    @ApiOperation(value = "业务分类排序", notes = "业务分类排序", httpMethod = "PUT")
    @PutMapping(value = "{id}/sort")
    public ApiResponse sort(@PathVariable String id,Integer offset) {
        Boolean sort = businessService.sort(id, offset);
        return ApiResponse.ofSuccess(sort);
    }



    @ApiOperation(value = "根据父级id获取业务分类集合", notes = "根据父级id获取业务分类集合", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pid", value = "父级id", required = true, paramType = "query")
    })
    @GetMapping("byPid")
    public ApiResponse<List<Business>> list( String pid) {

        List<Business> businessses = businessService.listByPid(pid);
        return ApiResponse.ofSuccess(businessses);

    }


    @ApiOperation(value = "业务集合", notes = "业务集合", httpMethod = "GET")
    @GetMapping
    public ApiResponse<List<Business>> list() {

        List<Business> businessses = businessService.list();

        return ApiResponse.ofSuccess(businessses);

    }
    @ApiOperation(value = "删除业务分类", notes = "删除业务分类", httpMethod = "DELETE")
    @DeleteMapping(value = "{id}")
    @SystemLog(description = "删除业务分类")
    public ApiResponse del(@PathVariable String id) {
        Boolean del = businessService.del(id);
        return ApiResponse.ofSuccess(del);
    }


    @ApiOperation(value = "获取业务详情", notes = "获取业务详情", httpMethod = "GET")
    @GetMapping(value = "{id}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "业务分类id", required = true, paramType = "path")
    })
    public ApiResponse<Business> get(@PathVariable String id) {

        Business business = businessService.get(id);
        return ApiResponse.ofSuccess(business);
    }

    @ApiOperation(value = "获取字段分组", notes = "获取字段分组", httpMethod = "GET")
    @GetMapping(value = "{id}/fields/{fieldName}/group")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "业务分类id", required = true, paramType = "path"),
            @ApiImplicitParam(name = "fieldName", value = "字段名称", required = true, paramType = "path"),
            @ApiImplicitParam(name = "archiveId", value = "存档", required = true, paramType = "query")
    })
    public ApiResponse<List<String>> getFieldValue(@PathVariable String id, @PathVariable String fieldName,String archiveId) {

        List<String> fieldValue = businessService.getFieldValue(id, fieldName,archiveId);
        return ApiResponse.ofSuccess(fieldValue);

    }


    @ApiOperation(value = "获取字段分组统计", notes = "获取字段分组统计", httpMethod = "GET")
    @GetMapping(value = "{id}/fields/{fieldName}/stat")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "业务分类id", required = true, paramType = "path"),
            @ApiImplicitParam(name = "fieldName", value = "字段名称", required = true, paramType = "path"),
            @ApiImplicitParam(name = "archiveId", value = "存档", required = true, paramType = "query")
    })
    public ApiResponse<List<GroupVO>> getFieldGroup(@PathVariable String id, @PathVariable String fieldName,@RequestParam Map<String, Object> param) {

        List<GroupVO> fieldGroup = businessService.getFieldGroup(id, fieldName, param);

        return ApiResponse.ofSuccess(fieldGroup);

    }


    @ApiOperation(value = "获取单个业务下面的单独字段统计", notes = "折线图使用 ", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "startTime", value = "开始时间 毫秒戳", required = true, paramType = "query"),
            @ApiImplicitParam(name = "endTime", value = "结束时间 毫秒戳", required = true, paramType = "query"),
            @ApiImplicitParam(name = "timeType", value = "统计维度 天day 月month", required = true, paramType = "query"),
            @ApiImplicitParam(name = "fieldName", value = "需要统计的字段名", required = true, paramType = "path"),
            @ApiImplicitParam(name = "businessId", value = "业务id", required = true, paramType = "path")
    })
    @GetMapping(value = "business/{businessId}/{fieldName}/stat")
    public ApiResponse<StatGroupDTO> getFieldNameDate(@RequestParam Map<String, Object> param, @PathVariable String businessId, @PathVariable String fieldName) {
        StatGroupDTO fieldNameDate = statisticService.getFieldNameDate( businessId, fieldName, param);
        return ApiResponse.ofSuccess(fieldNameDate);
    }


    @ApiOperation(value = "获取业务分类树形结构", notes = "获取业务分类树形结构", httpMethod = "GET")
    @GetMapping(value = "tree")
    public ApiResponse<List<BusinessVO>> getTree() {

        List<BusinessVO> businessVTree = businessService.getBusinessVTree();
        return ApiResponse.ofSuccess(businessVTree);
    }

    @ApiOperation(value = "根据名字获取业务", notes = "根据名字获取业务", httpMethod = "GET")
    @GetMapping(value = "byName")
    public ApiResponse<Business> getByName(String name) {
        Business byName = businessService.getByName(name);
        return ApiResponse.ofSuccess(byName);
    }



}
